
//
//  ModifyPasswordController.m
//  myapp
//
//  Created by 安建伟 on 2019/7/31.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "ModifyPasswordController.h"
#import "PhoneViewCell.h"
#import "ModifyPasswordViewCell.h"
#import "SureViewCell.h"

NSString *const CellIdentifier_Phone = @"CellID_Phone";
NSString *const CellIdentifier_ModifyPassword = @"CellID_ModifyPassword";
NSString *const CellIdentifier_Sure = @"CellID_Sure";

static CGFloat const HEIGHT_CELL = 51.f;
static float HEIGHT_HEADER = 40.f;

@interface ModifyPasswordController () <UITableViewDataSource,UITableViewDelegate,GizWifiSDKDelegate>

@property (strong, nonatomic) UITableView *modifyTable;

@property (nonatomic, strong) NSString *oldPwtext;
@property (nonatomic, strong) NSString *pwText;
@property (nonatomic, strong) NSString *pwConText;

@end

@implementation ModifyPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1.0];
    self.navigationItem.title = LocalString(@"修改密码");
    
    self.modifyTable = [self modifyTable];
    _oldPwtext = @"";
    _pwText = @"";
    _pwConText = @"";
    [GizWifiSDK sharedInstance].delegate = self;
}

#pragma mark - Lazy load
- (UITableView *)modifyTable{
    if (!_modifyTable) {
        _modifyTable = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            tableView.backgroundColor = [UIColor clearColor];
            tableView.dataSource = self;
            tableView.delegate = self;
            [tableView registerClass:[PhoneViewCell class] forCellReuseIdentifier:CellIdentifier_Phone];
            [tableView registerClass:[ModifyPasswordViewCell class] forCellReuseIdentifier:CellIdentifier_ModifyPassword];
            [tableView registerClass:[SureViewCell class] forCellReuseIdentifier:CellIdentifier_Sure];
            tableView.separatorColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.08];
            
            [self.view addSubview:tableView];
            
            tableView.scrollEnabled = NO;
            tableView;
        });
    }
    return _modifyTable;
}

#pragma mark - Giz delegate
// 实现回调
- (void)wifiSDK:(GizWifiSDK *)wifiSDK didChangeUserPassword:(NSError *)result {
    if(result.code == GIZ_SDK_SUCCESS) {
        // 修改成功
        [NSObject showHudTipStr:@"修改密码成功"];
        //返回上一级视图
        UIViewController *viewCtl =self.navigationController.viewControllers[1];
        [self.navigationController popToViewController:viewCtl animated:YES];
        
    } else {
        // 修改失败
        [NSObject showHudTipStr:@"修改密码失败"];
    } }

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0){
        return 4;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row ==0) {
        PhoneViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_Phone];
        if (cell == nil) {
            cell = [[PhoneViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier_Phone];
        }
        cell.phoneLabel.text = [GizManage shareInstance].userPhone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    ModifyPasswordViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_ModifyPassword];
    if (cell == nil) {
        cell = [[ModifyPasswordViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier_ModifyPassword];
    }
    if (indexPath.section == 0){
        if(indexPath.row == 1){
            cell.passwordTF.placeholder = LocalString(@"请输入原密码");
            cell.TFBlock = ^(NSString *text) {
                self.oldPwtext = text;
            };
            return cell;
        }else if(indexPath.row == 2){
            cell.passwordTF.placeholder = LocalString(@"请输入新密码");
            cell.TFBlock = ^(NSString *text) {
                self.pwText = text;
            };
            return cell;
        }else{
            cell.passwordTF.placeholder = LocalString(@"请再次确认密码");
            cell.TFBlock = ^(NSString *text) {
                self.pwConText = text;
            };
            return cell;
        }
    }else{
        SureViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_Sure];
        if (cell == nil) {
            cell = [[SureViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier_Sure];
        }
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1 && indexPath.row == 0 && ![_oldPwtext isEqualToString:@""] && ![_pwText isEqualToString:@""] && ![_pwConText isEqualToString:@""]){
        
        GizManage *manager = [GizManage shareInstance];
        [[GizWifiSDK sharedInstance] changeUserPassword:manager.token oldPassword:_oldPwtext newPassword:_pwConText];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT_CELL;
}

//section头部间距

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT_HEADER;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    view.backgroundColor = [UIColor clearColor];
    return view ;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

@end
